package com.douxg.fastCoding.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.douxg.fastCoding.spring.util.PageUtil;
import com.douxg.fastCoding.web.dto.DbInfoDto;
import com.douxg.fastCoding.web.entity.DbInfo;
import com.douxg.fastCoding.web.vo.DbInfoVo;
import com.douxg.fastCoding.web.mapper.DbInfoMapper;
import com.douxg.fastCoding.web.service.DbInfoService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Auther
 * @Date 2020-07-22 00:42:52
 */
@Service
public class DbInfoServiceImpl extends ServiceImpl<DbInfoMapper, DbInfo> implements DbInfoService {

    @Override
    public IPage<DbInfoVo> page(DbInfoDto dbInfoDto, Integer current, Integer size) {
        return page(dbInfoDto, current, size, true);
    }

    private IPage<DbInfoVo> page(DbInfoDto dbInfoDto, Integer current, Integer size, boolean searchCount) {
        Page<DbInfo> paramsPage = new Page<>();
        paramsPage.setCurrent(current);
        paramsPage.setSize(size);
        paramsPage.setSearchCount(searchCount);
        Page<DbInfo> dbPage = baseMapper.page(paramsPage, dbInfoDto);
        List<DbInfoVo> list = new ArrayList<>();
        for (DbInfo dbInfo : dbPage.getRecords()) {
            DbInfoVo dbInfoVo = new DbInfoVo();
            BeanUtil.copyProperties(dbInfo, dbInfoVo);
            list.add(dbInfoVo);
        }
        Page<DbInfoVo> resultPage = new Page<>();
        resultPage.setTotal(dbPage.getTotal());
        resultPage.setCurrent(dbPage.getCurrent());
        resultPage.setSize(dbPage.getSize());
        resultPage.setRecords(list);
        return resultPage;
    }

    @Override
    public IPage<DbInfoVo> pageWithProject(DbInfoDto dbInfoDto, Integer current, Integer size) {
        Page<Map<String, Object>> paramsPage = new Page<>();
        paramsPage.setCurrent(PageUtil.handlePageIndex(current));
        paramsPage.setSize(PageUtil.handlePageSize(size));
        paramsPage.setSearchCount(true);
        Page<Map<String, Object>> dbPage = baseMapper.pageWithProject(paramsPage, dbInfoDto);
        List<DbInfoVo> list = new ArrayList<>();
        for (Map<String, Object> map : dbPage.getRecords()) {
            DbInfoVo dbInfoVo = new DbInfoVo();
            BeanUtil.fillBeanWithMap(map, dbInfoVo, true, CopyOptions.create().setIgnoreCase(true));
            list.add(dbInfoVo);
        }
        Page<DbInfoVo> resultPage = new Page<>();
        resultPage.setTotal(dbPage.getTotal());
        resultPage.setCurrent(dbPage.getCurrent());
        resultPage.setSize(dbPage.getSize());
        resultPage.setRecords(list);
        return resultPage;
    }

    @Override
    public DbInfoVo queryOne(DbInfoDto dbInfoDto) {
        IPage<DbInfoVo> page = page(dbInfoDto, 1, 1, true);
        List<DbInfoVo> records = page.getRecords();
        if (records.size() > 0) {
            return records.get(0);
        }
        return null;
    }

    public List<DbInfoVo> myList(DbInfoDto dbInfoDto) {
        QueryWrapper<DbInfo> queryWrapper = getPageQueryWrapper(dbInfoDto, false);
        List<Map<String, Object>> maps = baseMapper.selectMaps(queryWrapper);
        List<DbInfoVo> list = coverToVos(maps);
        return list;
    }

    public Page<DbInfoVo> myPage(DbInfoDto dbInfoDto, Integer current, Integer size) {
        Page<Map<String, Object>> paramsPage = new Page<>();
        paramsPage.setCurrent(current);
        paramsPage.setSize(size);
        paramsPage.setSearchCount(true);
        QueryWrapper<DbInfo> queryWrapper = getPageQueryWrapper(dbInfoDto, false);
        Page<Map<String, Object>> dbPage = baseMapper.selectMapsPage(paramsPage, queryWrapper);
        List<DbInfoVo> list = new ArrayList<>();
        for (Map<String, Object> map : dbPage.getRecords()) {
            DbInfoVo dbInfoVo = new DbInfoVo();
            BeanUtil.fillBeanWithMap(map, dbInfoVo, true, false);
            list.add(dbInfoVo);
        }
        Page<DbInfoVo> resultPage = new Page<>();
        resultPage.setTotal(dbPage.getTotal());
        resultPage.setCurrent(dbPage.getCurrent());
        resultPage.setSize(dbPage.getSize());
        resultPage.setRecords(list);
        return resultPage;
    }

    private QueryWrapper<DbInfo> getPageQueryWrapper(DbInfoDto dbInfoDto, boolean orderBy) {
        QueryWrapper<DbInfo> queryWrapper = new QueryWrapper();
        Long projectId = dbInfoDto.getProjectId();
        if (projectId != null) {
            queryWrapper.eq("project_id", projectId);
        }
        if (orderBy) {
            queryWrapper.orderByDesc("");
        }
        return queryWrapper;
    }

    private DbInfoVo coverToVo(Map<String, Object> map) {
        DbInfoVo dbInfoVo = new DbInfoVo();
        BeanUtil.fillBeanWithMap(map, dbInfoVo, true, false);
        return dbInfoVo;
    }

    private List<DbInfoVo> coverToVos(List<Map<String, Object>> maps) {
        List<DbInfoVo> list = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            list.add(coverToVo(map));
        }
        return list;
    }
}
